package com.zys.el.user.service.Impl;

import com.zys.el.core.service.impl.CrudServiceImpl;
import com.zys.el.user.config.po.Show;
import com.zys.el.user.dao.UserDao;
import com.zys.el.user.po.FieldMenu;
import com.zys.el.user.po.Note;
import com.zys.el.user.po.User;
import com.zys.el.user.service.IFieldMenuService;
import com.zys.el.user.service.IFieldService;
import com.zys.el.user.service.INoteService;
import com.zys.el.user.service.IUserService;
import com.zys.el.user.util.FPGrowth.Myfptree2;
import com.zys.el.user.util.TFIDF.TF_IDF;
import com.zys.el.user.util.TFIDF.Word;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class UserServiceImpl extends CrudServiceImpl<User> implements IUserService {

    @Autowired
    IFieldService FiledService;
    @Autowired
    INoteService NoteService;
    @Autowired
    private IFieldMenuService fieldMenuService;
    @Autowired
    private Myfptree2 myfptree;
    @Autowired
    private TF_IDF tf_idf;
    @Autowired
    private IUserService userService;
    @Autowired
    private IUserService service;
    @Override
    public Long[] selectMenuByUser(User user) {
        return ((UserDao) getBaseMapper()).selectMenuByUser(user);
    }

    @Override
    public Integer selectCountFans(User user) {
        return ((UserDao) getBaseMapper()).selectCountFans(user);
    }

    @Override
    public Integer selectCountFollow(User user) {
        return ((UserDao) getBaseMapper()).selectCountFollow(user);
    }

    @Override
    public Show getUserNoteDataByUserId(Long id) {
        User byId = service.getById(id);
        List<List<String>> fpList = new ArrayList<>();
        List<List<String>> itemList = new ArrayList<>();
        List<Note> nOteList = NoteService.getNOteList(id);
        Show s = new Show();
        s.setId(id);
        s.setMotto(byId.getMotto());
        s.setFans_count(service.selectCountFans(byId));
        s.setFollow_count(service.selectCountFollow(byId));

        s.setNote_count(NoteService.getNoteCount(id));
        List<Show.note> list = new ArrayList<>();


        String idf = new String();
        for (Note note : nOteList) {
            idf += note.getContentPath();
        }


        //通过标题分词和tf-idf算法得到两种数据
        for (Note note : nOteList) {
            List<FieldMenu> fieldMenuByParentId = fieldMenuService.getFieldMenuByParentId(note.getId());
            //得到设计领域字段
            String[] strings = FiledService.selectUserFieldById(
                    (Long[]) fieldMenuByParentId.stream().map(FieldMenu::getFieldId).collect(Collectors.toList())
                            .toArray(new Long[fieldMenuByParentId.stream().map(FieldMenu::getFieldId).collect(Collectors.toList()).size()]));
            if (strings.length != 0) {
                itemList.add(Arrays.asList(strings));
            }

            List<Word> calcuate = tf_idf.calcuate(note.getContentPath(), idf, (double) nOteList.size());
            if (calcuate == null) {
                if (strings.length != 0) {
                    fpList.add(Arrays.asList(strings));
                } else {
                    fpList.add(Stream.of("无").collect(Collectors.toList()));
                }

            } else {
                if (calcuate.size() > 5) {
                    List<String> ca = new ArrayList<>();
                    for (int i = 0; i < 5; i++) {
                        ca.add(calcuate.get(i).getName());
                    }
                    fpList.add(ca);
                } else {
                    List<String> ca = new ArrayList<>();
                    for (int i = 0; i < calcuate.size(); i++) {
                        ca.add(calcuate.get(i).getName());
                    }
                    fpList.add(ca);
                }


            }

        }
        fpList.addAll(itemList);
//        System.out.println("fpList:" + fpList);
        List<String> nullString = new ArrayList<>();


        //tf-idf数据和当前数据进行频繁项集运算
        Set<String> strings = myfptree.FPTreeLL(fpList, nullString);
//        System.out.println("strings:" + strings);
        String[] strings1 = strings.toArray(new String[strings.size()]);


        Show.N N;
        int count = 0;
        for (Note temp : nOteList) {
            N = s.new N(temp.getId().toString(),
                    temp.getAuthorId().toString(),
                    temp.getCreateTime(),
                    "",
                    temp.getItem(),
                    temp.getFansMenuId(),
                    temp.getNoteItem());


            Set<String> prefield = new HashSet<>();
            if (fpList.get(count).size() >= 3) {
                prefield.add(fpList.get(count).get(0));
                prefield.add(fpList.get(count).get(1));
                prefield.add(fpList.get(count).get(2));
                if (strings.size() != 0) {
                    prefield.add(new ArrayList<>(strings).get(0));
                }
            } else {
                for (String stringList : fpList.get(count)) {
                    prefield.add(stringList);
                }
                if (strings.size() != 0) {
                    prefield.add(new ArrayList<>(strings).get(0));
                }
            }

            //String[] field = {"软工", "前端", "后端", "计算机技术"};

            Show.note note = s.new note(N,
                    fpList.get(count).size() >= 4 ? fpList.get(count).subList(0, 4).toArray(new String[4]) : fpList.get(count).subList(0, fpList.get(count).size() ).toArray(new String[fpList.get(count).size()]),
                    strings1.length != 0 ? strings1[0] : "云习");
            list.add(note);
            count++;
        }
        s.setNote(list);
        return s;
    }
}
